<template>
    <div class="layout-container">
        <div class="layout-container-left">
            <UseTable :tableData="tableData" multiple @change="change" />
        </div>

        <div class="layout-container-right">
            <div class="form-box">
                <el-form label-width="auto">
                    <el-form label-width="auto">
                        <el-form-item label="邻域半径">
                            <el-input v-model="paramsData.eps" type="number"  step="0.5"/>
                        </el-form-item>

                        <el-form-item label="最小点数">
                            <el-input v-model="paramsData.minPoints" type="number" />
                        </el-form-item>

                        <el-form-item>
                            <div class="data-select">
                                <DmData @dataChange="dataChange" />
                                <Upload @uploadSuccess="uploadSuccess" />
                            </div>
                        </el-form-item>

                        <el-form-item>
                            <el-button type="primary" @click="handleCalculate">计算</el-button>
                        </el-form-item>
                    </el-form>
                </el-form>
            </div>

            <el-divider />

            <div class="info-box">
                <h5>算法说明</h5>
                <p>基于数据密度划分簇，核心点（邻域内点数≥minPoints）形成簇，边界点依附于核心点，噪声点（非核心 / 边界点）被排除。</p>

                <h5>使用范围</h5>
                <ul>
                    <li>识别任意形状的簇（如环形、不规则分布）；</li>
                    <li>自动检测异常值（如离群的用户行为）；</li>
                    <li>需合理设置邻域半径（eps）和最小点数（minPoints），对参数较敏感。</li>
                </ul>
            </div>
        </div>

        <Result v-model="resultShow" :result="resultData" :originalData="originalData" />
    </div>
</template>

<script setup lang="ts">
import {onActivated, onMounted, ref} from "vue"
import {ElMessage}                   from "element-plus"
import Result                        from "./components/Result.vue"
import {getExcel}                    from "@/api/data"
import {density}                     from "@/api/group"
import Upload                        from "@/components/Upload.vue"
import UseTable                      from "@/components/UseTable.vue"
import DmData                        from "@/components/DmData.vue"

function dataChange(data) {
    tableData.value = data
}

function uploadSuccess(res) {
    tableData.value = res.data
}

let originalData = ref({
    listColumn: [],
    listAll   : []
})

function change(listColumn, listAll) {
    originalData.value.listColumn = listColumn
    originalData.value.listAll    = listAll

    paramsData.value.data = []

    let maxLength = Math.max(...listColumn.map(item => item.value.length))

    paramsData.value.data = Array.from({length: maxLength}, (_, index) =>
        listColumn.map(item => item.value[index])
    )
}

let paramsData = ref({
    data     : [],
    eps      : 0.5,
    minPoints: 5
})

let resultShow = ref(false)
let resultData = ref("")

function handleCalculate() {
    if (paramsData.value.data.length === 0) {
        ElMessage.warning("请选择数据")
        return
    }

    if (!paramsData.value.eps) {
        ElMessage.warning("请填写参数")
        return
    }

    if (!paramsData.value.minPoints) {
        ElMessage.warning("请填写参数")
        return
    }

    density(paramsData.value).then((res: any) => {
        resultData.value = res?.data
        resultShow.value = true
    })
}

let loading   = ref(true)
let tableData = ref([])

let getTableData = () => {
    loading.value = true

    getExcel("density").then(res => res.data).then(data => tableData.value = data)

    loading.value = false
}

/**
 * 初始化参数
 */
function initParam() {

}

// -----------------------------主程序start-----------------------------------------
onMounted(() => {
    initParam()
    getTableData()
})

onActivated(() => {
    initParam()
    getTableData()
})
// -----------------------------主程序end-----------------------------------------

</script>

<style scoped>

</style>

